﻿// -------------------------------------------------------------------------
//    @FileName         :    NFWorldPlayerModule.cpp
//    @Author           :    gaoyi
//    @Date             :    2025-02-27
//    @Email			:    445267987@qq.com
//    @Module           :    NFWorldPlayerModule
//
// -------------------------------------------------------------------------

#include "NFWorldPlayerModule.h"
#include "NFWorldData.h"
#include "NFWorldDataService.h"
#include "NFServerComm/NFServerCommon/NFIWorldServerModule.h"

NFWorldPlayerModule::NFWorldPlayerModule(NFIPluginManager* p) : NFMMODynamicModule(p)
{
}

NFWorldPlayerModule::~NFWorldPlayerModule()
{
}

int NFWorldPlayerModule::Awake()
{
    FindModule<NFIWorldServerModule>()->SetCheckStoreServer(true);
    NFServerConfig* pConfig = FindModule<NFIConfigModule>()->GetAppConfig(NF_ST_WORLD_SERVER);
    CHECK_NULL_RE_VAL(0, pConfig, false);

    RegisterAppTask(NF_ST_WORLD_SERVER, APP_INIT_DESC_STORE_LOAD, GetServerName(NF_ST_WORLD_SERVER) + " Load Desc Store", APP_INIT_TASK_GROUP_SERVER_LOAD_DESC_STORE);

    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetRunMode())
    {
        RegisterAppTask(NF_ST_WORLD_SERVER, APP_INIT_LOAD_GLOBAL_DATA_DB,
                        NF_FORMAT("{} {}", pConfig->ServerName, "Load World Global Data From Store Server"), APP_INIT_TASK_GROUP_SERVER_LOAD_OBJ_FROM_DB);
    }

    return 0;
}

int NFWorldPlayerModule::Tick()
{
    return NFIModule::Tick();
}

int NFWorldPlayerModule::OnDynamicPlugin()
{
    return NFIModule::OnDynamicPlugin();
}

int NFWorldPlayerModule::OnExecute(uint32_t serverType, uint32_t nEventID, uint32_t bySrcType, uint64_t nSrcID, const google::protobuf::Message* pMessage)
{
    return 0;
}

int NFWorldPlayerModule::OnHandleClientMessage(uint64_t unLinkId, NFDataPackage& packet)
{
    if (!m_pObjPluginManager->IsInited(NF_ST_WORLD_SERVER))
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "World Server not inited, drop client msg:{}", packet.ToString());
        return -1;
    }

    if (m_pObjPluginManager->IsServerStopping())
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "World Server is Stopping, drop client msg:{}", packet.ToString());
        return -1;
    }


    switch (packet.nMsgId)
    {
    default:
        {
            NFLogError(NF_LOG_DEFAULT, 0, "Client MsgId:{} Register, But Not Handle, Package:{}", packet.nMsgId, packet.ToString());
            break;
        }
    }
    return 0;
}

int NFWorldPlayerModule::OnHandleServerMessage(uint64_t unLinkId, NFDataPackage& packet)
{
    if (!m_pObjPluginManager->IsInited(NF_ST_WORLD_SERVER))
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "World Server not inited, drop client msg:{}", packet.ToString());
        return -1;
    }

    if (m_pObjPluginManager->IsServerStopping())
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "World Server is Stopping, drop client msg:{}", packet.ToString());
        return -1;
    }

    switch (packet.nMsgId)
    {
    default:
        {
            NFLogError(NF_LOG_DEFAULT, 0, "Server MsgId:{} Register, But Not Handle, Package:{}", packet.nMsgId, packet.ToString());
            break;
        }
    }
    return 0;
}

int NFWorldPlayerModule::AfterAllConnectAndAllDescStore(NF_SERVER_TYPE serverType)
{
    if (serverType != NF_ST_WORLD_SERVER) return 0;

    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetRunMode())
    {
        for (int i = WORLD_DATA_DB_NULL + 1; i < WORLD_DATA_DB_MAX_NUM; i++)
        {
            NFWorldData* pWorldData = NFWorldDataService::GetWorldData(i);
            if (pWorldData == nullptr)
            {
                LOG_ERR(0, -1, "NFWorldDataService::GetWorldData Failed, id:{}", i);
                return -1;
            }

            pWorldData->SetId(i);
            pWorldData->Load(NF_ST_WORLD_SERVER);
        }
    }

    return 0;
}
